<template>
  <div class="main-container">
    <van-cell-group v-if="user" class="my-info">
      <van-cell :border="false" class="base-info" center>
        <template #icon>
          <van-image class="avatar" round fit="cover"  :src="userInfo.data.photo" />
        </template>
        <template  #title><span class="nick-name">{{userInfo.data.name}}</span></template>
        <van-button class="edit-btn" size="small" round>编辑资料</van-button>
      </van-cell>
        <van-grid class="data-info" :border="false" :column-num="4">
        <van-grid-item class="data-info-item">
          <template #text>
            <div class="text-wrap">
              <span class="content">{{userInfo.data.art_count}}</span>
              <span class="text">头条</span>
            </div>
          </template>
        </van-grid-item>
        <van-grid-item class="data-info-item">
          <template #text>
            <div class="text-wrap">
              <span class="content">{{userInfo.data.follow_count}}</span>
              <span class="text">关注</span>
            </div>
          </template>
        </van-grid-item>
        <van-grid-item class="data-info-item">
          <template #text>
            <div class="text-wrap">
              <span class="content">{{userInfo.data.fans_count}}</span>
              <span class="text">粉丝</span>
            </div>
          </template>
        </van-grid-item>
        <van-grid-item class="data-info-item">
          <template #text>
            <div class="text-wrap">
              <span class="content">{{userInfo.data.like_count}}</span>
              <span class="text">获赞</span>
            </div>
          </template>
        </van-grid-item>
      </van-grid>
    </van-cell-group>
    <div v-else class="not-login">
      <div @click="router.push('/login')" class="">
        <img class="mobile" src="../../assets/images/avatar.png" alt="">
      </div>
      <div class="text">登录/注册</div>
    </div>
    <van-grid :border="false" class="mb-4 grid-nav"  :column-num="2">
      <van-grid-item  icon-prefix="toutiao" class="grid-nav-item" icon="shoucang" text="收藏"></van-grid-item>
      <van-grid-item  icon-prefix="toutiao" class="grid-nav-item" icon="lishi" text="历史"></van-grid-item>
    </van-grid>
    <van-cell title="消息通知" is-link to="" />
    <van-cell  class="mb-4" title="智能聊天机器人" is-link to="/user/chat" />
    <van-cell @click="onLogout" v-if="user" class="logout-cell" title="退出登录" />

    <van-loading v-if="isLoading" type="spinner" color="#1989fa" vertical>登录中...</van-loading>
      <!-- <van-dialog title="提示" @confirm="isLogout = true" @cancel="isLogout = false" message="确认退出吗?" v-model:show="isShow" show-cancel-button></van-dialog> -->
  </div>
</template>

<script lang="ts" setup>
import { computed,onMounted,ref,reactive, toRefs,nextTick, toRef,getCurrentInstance } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { Dialog  } from 'vant'
import { getCurrentUser } from 'api/user'
import { get } from 'vant/lib/utils'



const store = useStore()
const router = useRouter()
//@ts-ignore
const ctx = getCurrentInstance()
const isLoading = ref<boolean>(false)

let userInfo = reactive({
    data:{
      id:0,
      name:"昵称",
      art_count:10,
      certi:"",
      fans_count:100,
      follow_count:100,
      intro:"",
      is_media:false,
      photo:"",
      like_count:0,
    }
})

const user = computed(() => store.state.user.user)


console.log(user)


const getUserInfo = async () => {
  if(user.value.token) {
      try {
        let resp = await getCurrentUser()

        userInfo.data = resp.data.data
      } catch(err) {

      }
  }
}

onMounted(() => {
  getUserInfo()
})

const onLogout = () => {
  Dialog.confirm({
    title:"退出提示",
    message:"确定要退出?"
  }).then(() => {
    store.commit("setUser",null)
  }).catch(() => {

  })
}

</script>

<style lang="scss" scoped>
  .main-container {
    .my-info {
      background: url("assets/images/banner.png")no-repeat;

      .base-info {
        box-sizing: border-box;
        height: 115px;
        padding-top: 38px;
        padding-bottom: 11px;
        background-color: unset;

        .avatar {
          width: 66px;
          height: 66px;
          box-sizing: border-box;
          border: 1px solid #fff;
          margin-right: 11px;
        }

        .nick-name {
          color: #fff;
          font-size: 15px;
        }
        .edit-btn {
          color: #666;
          font-size: 10px;
          height: 16px;
        }
      }

      .data-info {

        .data-info-item {
          color: #fff;
          height: 65px;

          .text-wrap {
            display:flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            .content {
              font-size:18px;
            }

            .text {
              font-size:11px
            }
          }
        }
      }

      ::v-deep .van-grid-item__content {
        background-color: unset;
      }
    }

    ::v-deep .grid-nav {
        .grid-nav-item {
          height: 70px;

          .toutiao {
            font-size: 22px;
          }
          .toutiao-shoucang {
            color: #eb5253;
          }
          .toutiao-lishi {
            color: #ff9d1d;
          }

          .van-grid-item__text {
            font-size: 14px;
            color: #333;
          }
        }
      }

      .logout-cell {
        text-align: center;
        color: #d86262;
      }

      .mb-4 {
        margin-bottom: 4px;
      }

      .not-login {
        height: 180px;
        background:url("assets/images/banner.png")no-repeat;
        background-size: cover;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;

        .mobile {
          width: 66px;
          height: 66px;
        }
        .text {
          color:#fff;
          font-size:14px;
        }
      }
  }
</style>